package com.atguigu.gmall;

import javax.sound.midi.Soundbank;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 异步任务执行后,指定回调方法
 *
 * @author: atguigu
 * @create: 2023-02-28 15:40
 */
public class CompletableFutureTestAction {


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.创建异步操作对象 CompletableFuture对
        //2.需要获取线程执行结果
        CompletableFuture<String> futureResult = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t 异步任务执行,有返回值");
            //int i = 1 / 0;
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "atguigu";
        }).whenComplete((r,e)->{

        }).exceptionally((e) -> {
            System.out.println(Thread.currentThread().getName() + "\t 异步任务回调方法:" + e);
            return "6666";
        });

        String s = futureResult.get();
        System.out.println(s);
    }


}
